<!-- Copyright 2015 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -->
<!DOCTYPE html>
<title>Unit Test of e2e.openpgp.WorkerContextImpl</title>
<script src="../../../../../javascript/closure/base.js"></script>
<script src="test_js_deps-runfiles.js"></script>
<script>
  goog.require('e2e.async.ChildPeer');
  goog.require('e2e.async.Peer');
  goog.require('e2e.async.Result');
  goog.require('e2e.async.Service');
  goog.require('e2e.openpgp.WorkerContextImpl');
  goog.require('goog.string');
  goog.require('goog.testing.AsyncTestCase');
  goog.require('goog.testing.MockControl');
  goog.require('goog.testing.jsunit');
  goog.require('goog.testing.storage.FakeMechanism');
</script>
<script>
var stubs = null;
var asyncTest = goog.testing.AsyncTestCase.createAndInstall(document.title);
var workerContext = null;
var localContextPromise = null;
var FakeService;
var fakeNetwork;
var servicePeer;
var clientPeer;

function setUp() {
  stubs = new goog.testing.PropertyReplacer();
  fakeNetwork = new MessageChannel();
  servicePeer = new e2e.async.ChildPeer();
  servicePeer.addPort(fakeNetwork.port1);
  clientPeer = new e2e.async.Peer();
  clientPeer.addPort(fakeNetwork.port2);
  clientPeer.init = servicePeer.init = goog.nullFunction;

  FakeService = function(port) {
    FakeService.base(this, 'constructor', port);
  }
  goog.inherits(FakeService, e2e.async.Service);

  servicePeer.registerService(e2e.openpgp.WorkerContextImpl.SERVICE_NAME,
      FakeService);
  localContextPromise = e2e.openpgp.WorkerContextImpl.launch(clientPeer);
}

function tearDown() {
  stubs.reset();
}

function testNoParam() {
  var dummyResult = 'yay!';
  stubs.set(FakeService.prototype, '_public_hasPassphrase', function() {
    return e2e.async.Result.toResult(dummyResult);
  });

  asyncTest.waitForAsync('Wait for local context being ready.');
  localContextPromise.addCallbacks(function(localContext) {
    asyncTest.waitForAsync('Wait for hasPassphrase callback');
    localContext.hasPassphrase().addCallbacks(function(result) {
      assertEquals(result, dummyResult);
      asyncTest.continueTesting();
    }, fail);
  }, fail);
}

function testSyncResult() {
  var dummyResult = 'yay!';
  stubs.set(FakeService.prototype, '_public_hasPassphrase', function() {
    return dummyResult;
  });
  asyncTest.waitForAsync('Wait for local context being ready.');
  localContextPromise.addCallbacks(function(localContext) {
    asyncTest.waitForAsync('Wait for hasPassphrase callback');
    localContext.hasPassphrase().addCallback(function(result) {
      assertEquals(result, dummyResult);
      asyncTest.continueTesting();
    }, fail);
  }, fail);
}


function testWithParams() {
  var param = 1337;
  var param2 = 'Second parameter';
  stubs.set(FakeService.prototype, '_public_encryptSign', function(p1, p2) {
    assertEquals(param, p1);
    assertEquals(param2, p2);
    return p1;
  });
  asyncTest.waitForAsync('Wait for local context being ready.');
  localContextPromise.addCallbacks(function(localContext) {
    asyncTest.waitForAsync('Wait for encryptSign callback');
    localContext.encryptSign(param, param2).addCallbacks(function(result) {
      assertEquals(param, result);
      asyncTest.continueTesting();
    }, fail);
  }, fail);
}


function testFunctionCallbacks() {
  var param = 1337;
  var param2 = 'Second parameter';
  stubs.set(FakeService.prototype, '_public_importKey', function(
      passphraseCallback, key) {
    var result = new e2e.async.Result();
    passphraseCallback('dummy').addCallbacks(function(res) {
      result.callback('nested(' + res + ')');
    }, fail);
    return result;
  });
  asyncTest.waitForAsync('Wait for local context being ready.');
  localContextPromise.addCallbacks(function(localContext) {
    asyncTest.waitForAsync('Wait for importKey callback');
    var passphraseCallback = function(p) {
      return 'fromcb(' + p + ')';
    };
    localContext.importKey(passphraseCallback, undefined).addCallbacks(
        function(result) {
      assertEquals('nested(fromcb(dummy))', result);
      asyncTest.continueTesting();
    }, fail);
  }, fail);
}

</script>
